package cn.bud.msgbus.server.config;

import cn.bud.msgbus.server.busdao.BusDao;
import cn.bud.msgbus.server.properties.Zl9cMsgBusClientConfig;
import cn.bud.msgbus.server.properties.Zl9cMsgBusServerConfig;
import cn.bud.msgbus.server.server.MsgBusDataQueue;
import cn.bud.msgbus.server.server.MsgBusServer;
import cn.bud.msgbus.server.server.subscribe.MsgBusClient;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * BusConfig
 *
 * @author hxj
 * @version 1.0
 * @description
 * @date 2024-06-19 10:25
 */
@Configuration
public class BusConfig {

    @Resource
    private Zl9cMsgBusServerConfig msgBusConfig;
    @Resource
    private Zl9cMsgBusClientConfig msgBusClientConfig;
    @Resource
    private BusDao busDao;
    @Resource
    private MsgBusDataQueue msgBusDataQueue;

    MsgBusServer server;
    MsgBusClient client;


    @PostConstruct
    public void init() {
        // 初始化服务端
        initServer();
        // 初始化客户端
        initClient();

    }

    @PreDestroy
    public void stop() {
        if (server != null) {
            server.stop();
        }
    }

    private void initClient() {
//        client =
    }

    private void initServer() {
        if (!msgBusConfig.getEnable()) {
            return;
        }
        server = new MsgBusServer.MsgBusServerBuilder()
                .serverMode(msgBusConfig.getMode())
                .nettyPorts(msgBusConfig.getNettyPorts())
                .msgBusDataQueue(msgBusDataQueue)
                .subscribers(busDao.listSubscriber())
                .build();

        server.start();

    }

    public MsgBusServer busServer() {
        return server;
    }


}
